/
index.js
276 lines (254 loc) 路 8.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import NextLink from "next/link";
import Head from "next/head";
import { useRouter } from "next/router";
import { useEffect } from "react";
import {
Text,
VStack,
Image,
HStack,
useColorMode,
Center,
Spinner,
useToast,
Heading,
} from "@chakra-ui/react";
import { useWebsite } from "@/providers/WebsiteProvider";
import { useWebsiteControls } from "@/hooks/services/website/useWebsiteControls";
import { usePaymentControls } from "@/hooks/usePaymentControls";
import Template1 from "@/components/services/Website/SiteTemplates/Template1";
import Template2 from "@/components/services/Website/SiteTemplates/Template2";
import Template3 from "@/components/services/Website/SiteTemplates/Template3";
import Template4 from "@/components/services/Website/SiteTemplates/Template4";
import Template5 from "@/components/services/Website/SiteTemplates/Template5";
import Template6 from "@/components/services/Website/SiteTemplates/Template6";
import Template7 from "@/components/services/Website/SiteTemplates/Template7";
import Template8 from "@/components/services/Website/SiteTemplates/Template8";
import Template9 from "@/components/services/Website/SiteTemplates/Template9";
import Template10 from "@/components/services/Website/SiteTemplates/Template10";
import Template11 from "@/components/services/Website/SiteTemplates/Template11";
import errorHandler from "@/utils/errorHandler";
import config from "@/config/index";
import parse from "html-react-parser";
import axios from "axios";
import posthog from "posthog-js";
const UserWebsite = (props) => {
const toast = useToast({
title: "Error",
status: "error",
duration: 3000,
isClosable: true,
position: "bottom",
});
const router = useRouter();
const { userWebsite, setUserWebsite } = useWebsite();
const { userWebsiteErrors, checkSubscription, setUserWebsiteErrors } =
useWebsiteControls();
useEffect(() => {
if (!props) return;
const renderWebsite = async () => {
try {
let newUserWebsiteErrors = [];
if (!Object.keys(props).length)
newUserWebsiteErrors.push(`Minting Website was not found`);
else {
const {
isExpired,
isPublished,
components: { title },
} = props;
if (isExpired)
newUserWebsiteErrors.push(
`Error: '${title}' Minting Website has Expired. Go to website settings -> General -> Renew`,
);
if (!isPublished)
newUserWebsiteErrors.push(
`Error: '${title}' Minting Website is not Published yet. Go to website settings -> Advanced -> Publish`,
);
}
if (newUserWebsiteErrors.length > 0) {
setUserWebsiteErrors(newUserWebsiteErrors);
throw new Error(
"If you are the owner of this minting website, please check your site settings",
);
}
setUserWebsite(props);
} catch (err) {
const msg = errorHandler(err);
toast({ description: msg });
}
};
renderWebsite();
}, []);
useEffect(() => {
if (!userWebsite) return;
checkSubscription();
console.log("Website ID:", userWebsite._id);
posthog.capture("User visited minting website", {
route: userWebsite.route,
referrer: document.referrer,
});
}, [userWebsite]);
const { colorMode } = useColorMode();
if (router.isFallback) {
return (
<Center style={{ minHeight: "100vh" }}>
<Spinner
thickness="4px"
speed="0.65s"
emptyColor="gray.200"
color="rgb(117,63,229)"
size="lg"
/>
</Center>
);
}
return (
<>
{userWebsite ? (
<div>
<Head>
<title>{userWebsite?.components?.title}</title>
<link
rel="shortcut icon"
type="image/png"
href={userWebsite?.meta?.favicon}
/>
<meta name="title" content={userWebsite?.components?.title} />
<meta
name="description"
content={userWebsite?.components?.description}
/>
<meta
name="keywords"
content={`nfthost, ${userWebsite?.components?.title}`}
/>
<meta name="robots" content={userWebsite?.meta?.robot} />
<meta httpEquiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content={userWebsite?.meta?.language} />
<meta property="og:type" content="website" />
<meta
property="og:url"
content={`https://${userWebsite?.route}.${config.frontendUrl}`}
/>
<meta property="og:title" content="NFT Host" />
<meta
property="og:description"
content={userWebsite?.components?.description}
/>
<meta
property="og:image"
content={userWebsite?.components?.unrevealedImage}
/>
<meta property="twitter:card" content="summary_large_image" />
<meta
property="twitter:url"
content={`https://${userWebsite?.route}.${config.frontendUrl}`}
/>
<meta
property="twitter:title"
content={userWebsite?.components?.title}
/>
<meta
property="twitter:description"
content={userWebsite?.components?.description}
/>
<meta
property="twitter:image"
content={userWebsite?.components?.unrevealedImage}
/>
{userWebsite?.components?.script &&
parse(userWebsite?.components?.script)}
</Head>
<main>
{
{
Template1: <Template1 />,
Template2: <Template2 />,
Template3: <Template3 />,
Template4: <Template4 />,
Template5: <Template5 />,
Template6: <Template6 />,
Template7: <Template7 />,
Template8: <Template8 />,
Template9: <Template9 />,
Template10: <Template10 />,
Template11: <Template11 />,
}[userWebsite?.components?.template]
}
</main>
</div>
) : (
<Center style={{ minHeight: "100vh" }}>
{userWebsiteErrors?.length > 0 ? (
<VStack spacing="1em">
<NextLink href="/" shallow passHref>
<HStack spacing=".5em" cursor="pointer" flex="1">
<Image src="/assets/logo.png" alt="NFT Host Logo" w="50px" />
<Heading
as="h1"
fontWeight="bold"
fontFamily="inter"
fontSize="20pt"
>
NFT Host
</Heading>
</HStack>
</NextLink>
<VStack>
{userWebsiteErrors?.map((err, idx) => (
<Text key={idx} fontSize="10pt">
{err}
</Text>
))}
</VStack>
</VStack>
) : (
<Spinner
thickness="4px"
speed="0.65s"
emptyColor="gray.200"
color="rgb(117,63,229)"
size="lg"
/>
)}
</Center>
)}
</>
);
};
export async function getStaticPaths() {
const mappedSubdomains = await fetch(
`${config.serverUrl}/api/website/getMappedSubdomains`,
{
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `bearer ${process.env.CREATE_WEBSITE_TOKEN}`,
},
},
);
return {
paths: await mappedSubdomains.json(),
fallback: true,
};
}
export async function getStaticProps({ params: { siteRoute } }) {
const site = await fetch(
`${config.serverUrl}/api/website/getWebsiteByRoute?route=${siteRoute}`,
{
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `bearer ${process.env.CREATE_WEBSITE_TOKEN}`,
},
},
);
return {
props: await site.json(),
revalidate: 30,
};
}
export default UserWebsite;